<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/27 0027
 * Time: 17:10
 */

namespace app\common\model;
use think\Db;
use think\Model;
use app\api\controller\Base;

class Gift extends Model
{

    /**
     * 获取用户礼物背包数量
     * @return mixed
     */
//    public function getNumAttr()
//    {
//        return 0;
//    }

//    protected $append = ['num'];


    public function giftList($where = [],$type = false,$user_id)
    {
        $rows =  $this
            ->where($where)
            ->field('gift_id,gift_name,thumbnail,img,price,is_push,image_size,origin_x,origin_y,image_width,image_height')
            ->order('price')
            ->cache(30)
            ->select();

        if ($type == 1 && $user_id != 0){ //不获取背包剩余礼物数
            $map['user_id'] = $user_id;
            $map['type']    = 1;
            $package = Db::name('my_package')->where($map)->field('num,key')->select();
            $array = array_key($package,'key');
            $num = true;
        }


        foreach ($rows as $k => $v){
            if ($num){
                $rows[$k]['num'] = $array[$v['gift_id']]['num']??0;
            }
            $rows[$k]['thumbnail'] = getThumb($v['thumbnail'],300);
        }

        return $rows;
    }



    //后台获取礼物列表
    public function giftListAdmin($where = [])
    {
        return $this
            ->where($where)
            ->order('price')
            ->paginate('',false,['query'=>request()->param()]);

    }


    public function saveChange($data){
        if(is_numeric($data['id'])){
            return $this->allowField(true)->isUpdate(true)->save($data,['gift_id'=>$data['id']]);
        }else{
            return $this->allowField(true)->save($data);
        }
    }









}